package com.moive.news.tqnewsapplication.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.moive.news.tqnewsapplication.R;
import com.moive.news.tqnewsapplication.adapter.NewsHisAdapter;
import com.moive.news.tqnewsapplication.base.BaseActivity;
import com.moive.news.tqnewsapplication.entity.NewsHistInfo;
import com.moive.news.tqnewsapplication.entity.NewsInfo;
import com.moive.news.tqnewsapplication.networks.BaseApi;
import com.moive.news.tqnewsapplication.okmanager.OkManagerCallback;
import com.moive.news.tqnewsapplication.utils.GetSystemTimeUitl;
import com.moive.news.tqnewsapplication.utils.LogUtil;
import com.moive.news.tqnewsapplication.utils.PreferenceUtil;
import com.moive.news.tqnewsapplication.utils.ToastUtils;
import com.moive.news.tqnewsapplication.weight.mylistview.SecondXListView;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/12/22.
 * 投稿记录
 */

public class NewsHistorActivity extends BaseActivity implements SecondXListView.IXListViewListener {
    @BindView(R.id.his_back)
    ImageView hisBack;
    @BindView(R.id.pub_all_listview)
    SecondXListView pubAllListview;
    @BindView(R.id.pub_all_myText)
    TextView pubAllMyText;
    private Gson gson = new Gson();
    private int page = 1;
    private List<NewsInfo.RowsBean> list = new ArrayList<>();
    NewsHisAdapter ad;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newshistor);
        ButterKnife.bind(this);
        init();
    }
    private void getdata() {
        OkHttpUtils.get()
                .url(BaseApi.PUTNEWS_HISTOR)
                .addParams("token", PreferenceUtil.getString("token", ""))
                .addParams("page", page + "")
                .addParams("rows", "10")
                .addParams("category","糗事")
                .build().execute(new OkManagerCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.e(TAG, e.toString());
            }
            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.i(TAG, response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.optInt("code") == 0) {
                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                        if (jsonArray != null) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject1 = (JSONObject) jsonArray.opt(i);
                                NewsInfo.RowsBean b = gson.fromJson(jsonObject1 + "", NewsInfo.RowsBean.class);
                                list.add(b);
                            }
                            ad.addItems(list);
                        }if (jsonArray.length()==0){
                            ToastUtils.ShortToast("没有更多数据了！");
                        }
                        pubAllListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent i = new Intent(NewsHistorActivity.this, NewsDteilsAcitivity.class);
                                Bundle b = new Bundle();
                                b.putString("url", list.get(position-1).getId());
                                LogUtil.i("lidaming",BaseApi.NEWS_TYPE_ITEM_TYPE+ list.get(position-1).getId());
                                i.putExtras(b);
                                startActivity(i);
                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void init() {
        ad = new NewsHisAdapter(this);
        pubAllListview.setAdapter(ad);
        pubAllListview.setXListViewListener(this);
        pubAllListview.setPullLoadEnable(true);
        getdata();
        hisBack.setOnClickListener(v -> finish());
    }

    @Override
    public void onRefresh() {
        page = 1;
        list.clear();
        getdata();
        ad.notifyDataSetChanged();
        stopOnLoad();
    }

    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                page++;
                getdata();
                ad.notifyDataSetChanged();
                stopOnLoad();
            }
        }, 1000);
    }
    private void stopOnLoad() {
        pubAllListview.stopRefresh();
        pubAllListview.stopLoadMore();
        pubAllListview.setRefreshTime(GetSystemTimeUitl.getTime());
    }
}
